package com.cskaoyan.controller;

import com.cskaoyan.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author stone
 * @date 2021/12/30 16:22
 */
@Controller
public class HelloController {

    @RequestMapping("hello") //没有写@ResponseBody
    public ModelAndView hello() {
        //想要访问webapp路径下hello.jsp 并且给里面的username赋值
        ModelAndView modelAndView = new ModelAndView();
        // 通过ModelAndView要设定访问视图名为hello.jsp
        modelAndView.setViewName("/hello.jsp");
        // 通过ModelAndView要设定username对应的值
        modelAndView.addObject("username", "songge");

        return modelAndView;
    }

    //返回值字符串作为视图名
    @RequestMapping("hello2")
    public String hello2(Model model) {
        //相当于
        //ModelAndView modelAndView = new ModelAndView();
        //modelAndView.setViewName("/hello.jsp");
        model.addAttribute("username", "ligenli"); //和上面的hello方法做的事情是一样的
        return "/hello.jsp";
    }

    // 如果增加了@ResponseBody → 结果就是字符串
    @RequestMapping("hello3")
    @ResponseBody
    public String hello3() {

        return "/hello.jsp";
    }

    @RequestMapping("hello/json")
    @ResponseBody
    public User helloJson() {
        User user = new User();
        user.setUsername("松哥");
        user.setPassword("李艮隶");
        return user;
    }
}
